import { produce } from "immer";
import { create } from "zustand";

type Action = {
  addTagsView: (tagView: TagView) => void;
  deleteTagsView: (tagView: TagView) => Promise<TagView[]>;
};

interface State {
  tagsView: TagView[]; // 资源
}

export const useTagsViewStore = create<State & Action>()((set) => ({
  tagsView: [],
  addTagsView: (view) =>
    set(
      produce((state: State) => {
        if (state.tagsView.some((v) => v.path === view.path)) return;
        state.tagsView = [...state.tagsView, view];
      })
    ),
  deleteTagsView: (view) => {
    return new Promise((resolve) => {
      set(
        produce((state: State) => {
          state.tagsView = state.tagsView.filter((v) => v.path !== view.path);
          resolve(state.tagsView);
        })
      );
    });
  },
}));
